﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlServerCe;

namespace ProjectoPV_ESW.Account.Relatorio
{
    /// <summary>
    /// Classe desenvolvida por Mário Pereira.
    /// </summary>
    public partial class CriarRelatorio : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Label1.Text = "";
            this.LabelOK.Text = "";
        }

        protected void ButtonAdicionarRelatorio_Click(object sender, EventArgs e)
        {
            string CriancaID = DropDownList1.SelectedItem.Value;
            int dia = Calendar1.SelectedDate.Day;
            int mes = Calendar1.SelectedDate.Month;
            int ano = Calendar1.SelectedDate.Year;
            string relatorio = TextBox1.Text;

            if (Calendar1.SelectedDate.DayOfWeek == DayOfWeek.Sunday || Calendar1.SelectedDate.DayOfWeek == DayOfWeek.Saturday)
            {
                this.Label1.Text = "Erro, não existem dias escolares no fim de semana, logo não existem relatórios.";
            }
            else
            {
                if (Calendar1.SelectedDate.Year == 1 || TextBox1.Text == "")
                {
                    this.Label1.Text = "Erro, por favor preencha todos os dados!";
                }

                else
                {

                    string connectionStringJardim = ConfigurationManager.ConnectionStrings["JardimEntitiesString"].ConnectionString;
                    string commandText = @"INSERT INTO RelatorioEducando(Crianca_CriancaId,dia,mes,ano,Conteudo) VALUES(@Crianca_CriancaId,@dia,@mes,@ano,@Conteudo)";
                    SqlCeConnection conn = new SqlCeConnection(connectionStringJardim);
                    string selectCountCommand = @"SELECT COUNT(Conteudo) FROM RelatorioEducando WHERE [dia]= @dia AND [mes] = @mes  AND [ano] = @ano  AND [Crianca_CriancaId] = @crianca ";
                    SqlCeCommand selectCommand = new SqlCeCommand(selectCountCommand, conn);
                    selectCommand.Parameters.AddWithValue("@dia", dia);
                    selectCommand.Parameters.AddWithValue("@mes", mes);
                    selectCommand.Parameters.AddWithValue("@ano", ano);
                    selectCommand.Parameters.AddWithValue("@crianca", CriancaID);
                    int numeroLinhas = 0;
                    try
                    {
                        conn.Open();
                        numeroLinhas = (int)selectCommand.ExecuteScalar();
                        
                    }
                    catch (Exception ex)
                    {
                        Label1.Text = "Erro-" + ex.Source + "-" + ex.Message;
                    }
                    finally { conn.Close(); }
                    if (numeroLinhas == 0)
                    {
                        try
                        {
                            conn.Open();
                            SqlCeCommand insertCommand = new SqlCeCommand(commandText, conn);
                            insertCommand.Parameters.AddWithValue("@Crianca_CriancaId", CriancaID);
                            insertCommand.Parameters.AddWithValue("@dia", dia);
                            insertCommand.Parameters.AddWithValue("@mes", mes);
                            insertCommand.Parameters.AddWithValue("@ano", ano);
                            insertCommand.Parameters.AddWithValue("@Conteudo", relatorio);
                            int rows = insertCommand.ExecuteNonQuery();
                            this.LabelOK.Text = "Relatório inserido!";
                            insertCommand.Parameters.Clear();
                            insertCommand.Dispose();
                        }
                        catch (Exception ex)
                        {
                            Label1.Text = "Erro-" + ex.Source + "-" + ex.Message;
                        }
                        finally
                        {
                            TextBox1.Text = "";
                            conn.Close();
                            //Label bom - Inserção Feita
                        }
                    }
                    else { Label1.Text = "Já existe um relatório neste dia!"; }
                }

            }
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBox1.Text = "";
        }


        }
    }
